/**
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.profile;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.mode.response.BaseResponse;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
@EFragment(R.layout.modify_password)
public class ModifyPasswordFragment extends BaseFragment{
	final static String TAG = "modify_password";
	public ModifyPasswordFragment() {
		super();
	}
	
	@ViewById(R.id.modify_password_root)
	LinearLayout mRootLayout;
	
	@ViewById(R.id.modify_password_old)
	EditText mOldExt;
	@ViewById(R.id.modify_password_new)
	EditText mNewExt;
	@ViewById(R.id.modify_password_confirm)
	EditText mConfirmExt;
	
	@ViewById(R.id.modify_password_action)
	TextView mModifyAction;
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;
	String mOldStr;
	String mNewStr;
	String mConfirmStr;
//	String mMobile;
//	String mValidateNo;

	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
//		args.putString(CONSTANT.ARGS.MOBILE, mobile);
//		args.putString(CONSTANT.ARGS.VERIFY_CODE, validateNo);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,ModifyPasswordFragment_.class.getName(),args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		mRootLayout.setOnClickListener(null);
	}
	
	@Click({R.id.modify_password_back,R.id.modify_password_action})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.modify_password_back:
			backAction(mFragmentId);
			break;
		case R.id.modify_password_action:
			modifyPassword();
			break;
		default:
			break;
		}
		showSoftKeyBoard(mOldExt, false);
		showSoftKeyBoard(mConfirmExt, false);
		showSoftKeyBoard(mNewExt, false);
	}
	
	public void modifyPassword(){
		if (TextUtils.isEmpty(mConfirmExt.getText()) || TextUtils.isEmpty(mNewExt.getText())) {
			BaseApplication.showToast(R.string.reset_password_not_null);
			return;
		}else{
			mOldStr = mOldExt.getText().toString();
			mNewStr = mNewExt.getText().toString();
			mConfirmStr = mConfirmExt.getText().toString();
		}
		if (!mNewStr.equals(mConfirmStr)) {
			BaseApplication.showToast(R.string.regist_error_password_not_confirmed);
		}
		BaseJsonHandler<BaseResponse> response = new BaseJsonHandler<BaseResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, BaseResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					BaseApplication.showToast("修改密码成功");
					backAction(mFragmentId);
                } else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected BaseResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,BaseResponse.class);
			}
		};
		HttpManager.getInstance().modifyPassword(BaseApplication.mUserNo, mOldStr, mNewStr, response);
	}
	

	@Override
	public void clear() {
		showSoftKeyBoard(mOldExt, false);
		showSoftKeyBoard(mConfirmExt, false);
		showSoftKeyBoard(mNewExt, false);
		
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
//			mMobile = args.getString(CONSTANT.ARGS.MOBILE);
//			mValidateNo = args.getString(CONSTANT.ARGS.VERIFY_CODE);
		}
	}

}
